package a07_二叉树;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * a02_二叉树的递归遍历复习2
 * </p>
 *
 * @author flyduck
 * @since 2025/1/2
 */
public class a02_二叉树的递归遍历复习2 {



    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        traversal1(root, result);
        return result;
    }

    private void traversal1(TreeNode currentNode,List<Integer> result){
        if (currentNode == null) {
            return;
        }
        result.add(currentNode.val);
        traversal1(currentNode.left, result);
        traversal1(currentNode.right, result);
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        traversal2(root, result);
        return result;
    }

    private void traversal2(TreeNode currentNode,List<Integer> result){
        if (currentNode == null) {
            return;
        }
        traversal2(currentNode.left, result);
        result.add(currentNode.val);
        traversal2(currentNode.right, result);
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        traversal3(root, result);
        return result;
    }

    private void traversal3(TreeNode currentNode,List<Integer> result){
        if (currentNode == null) {
            return;
        }
        traversal3(currentNode.left, result);
        traversal3(currentNode.right, result);
        result.add(currentNode.val);
    }
}
